import { Component, OnInit } from '@angular/core';
import {LeaveRecord, BasicInformation} from '../../domain/admin';
import {ServiceLogicService} from '../../service/service-logic.service';
import {ActivatedRoute} from '@angular/router';

@Component({
  selector: 'app-leave-application',
  templateUrl: './leave-application.component.html',
  styleUrls: ['./leave-application.component.css']
})
export class LeaveApplicationComponent implements OnInit {
  judge: Boolean = false;
  displayDialog: boolean;
  selectedLeaveRecord: LeaveRecord;
  newLeaveRecord: boolean;
  private data: any;
  cols: any[];
  basicInformation: BasicInformation;
  approved_LeaveRecords: LeaveRecord[];
  rejectAndPass_LeaveRecords: LeaveRecord[];
  leaveRecord: LeaveRecord = {};
  onRowSelect(event) {
    this.newLeaveRecord = false;
    this.leaveRecord = this.cloneOutRecord(event.data);
    this.displayDialog = true;
  }
  showDialogToAdd() {
    this.newLeaveRecord = true;
    this.leaveRecord = {};
    this.displayDialog = true;
  }
  save() {
    const outRecords = [...this.approved_LeaveRecords];
    if (this.newLeaveRecord) {
      outRecords.push(this.leaveRecord);
      console.log(this.leaveRecord);
      console.log(12);
      this.leaveRecord.state = 1;
      this.leaveRecord.des = '审核中';
      this.panduan();
      const faultLeaveRecord = {
        admin_id: this.data.admin_id,
        satrt_time: this.leaveRecord.satrt_time,
        end_time: this.leaveRecord.end_time,
        reason: this.leaveRecord.reason,
        days: parseInt(this.leaveRecord.days, 0),
        type: parseInt(this.leaveRecord.type, 0),
        state: parseInt(this.leaveRecord.state, 0),
        des: this.leaveRecord.des
      };
      console.log(22222);
      console.log(this.judge);
      if (this.judge) {
        this.logicService.addLeaveRecord(faultLeaveRecord).then(res => { outRecords[outRecords.length - 1].id = res.id; });
      } else {
        alert('超出剩余请假日期');
      }
      // 在这里像数据库写入数据。
    } else {
      outRecords[this.approved_LeaveRecords.indexOf(this.selectedLeaveRecord)] = this.leaveRecord;
      this.panduan();
      const faultLeaveRecord = {
        admin_id: this.leaveRecord.admin_id,
        satrt_time: this.leaveRecord.satrt_time,
        end_time: this.leaveRecord.end_time,
        reason: this.leaveRecord.reason,
        days: parseInt(this.leaveRecord.days, 0),
        type: parseInt(this.leaveRecord.type, 0),
        state: parseInt(this.leaveRecord.state, 0),
        des: this.leaveRecord.des
      };
      switch (faultLeaveRecord.type) {
        case 0:
          if (faultLeaveRecord.days <= parseInt(this.basicInformation[0].compassionate_leave, 0)) { this.judge = true; }
          break;
        case 1:
          if (faultLeaveRecord.days <= parseInt(this.basicInformation[0].sick_leave, 0)) { this.judge = true; }
          break;
        case 2:
          if (faultLeaveRecord.days <= parseInt(this.basicInformation[0].marriage_leave[0], 0)) { this.judge = true; }
          break;
        case 3:
          if (faultLeaveRecord.days <= parseInt(this.basicInformation[0].maternity_leave[0], 0)) { this.judge = true; }
          break;
        case 4:
          if (faultLeaveRecord.days <= parseInt(this.basicInformation[0].annual_leave[0], 0)) { this.judge = true; }
          break;
        default:
          break;
      }
      if (this.judge) {
        this.logicService.addLeaveRecord(faultLeaveRecord).then(() => {});
      } else {
        console.log(1231);
        alert('超出剩余请假日期');
      }
    }
    if (this.judge) {
      this.approved_LeaveRecords = outRecords;
      this.leaveRecord = null;
      this.displayDialog = false;
      this.judge = false;
    }
  }
  panduan() {
    console.log(111);
    switch (this.leaveRecord.type_name) {
      case '事假':
        console.log('事假');
        this.leaveRecord.type = 0;
        // console.log(12344444444);
        // console.log(this.basicInformation[0].compassionate_leave);
        // console.log(parseInt(this.leaveRecord.days, 0));
        console.log(11111);
        console.log(this.leaveRecord.days);
        console.log(this.basicInformation[0].compassionate_leave);
        if (parseInt(this.leaveRecord.days, 0) <= parseInt(this.basicInformation[0].compassionate_leave, 0)) {
          this.judge = true;
          // console.log(this.judge);
        }
        break;
      case '病假':
        console.log('病假');
        this.leaveRecord.type = 1;
        if (parseInt(this.leaveRecord.days, 0) <= parseInt(this.basicInformation[0].sick_leave, 0)) {
          this.judge = true;
        }
        break;
      case '婚假':
        console.log('婚假');
        this.leaveRecord.type = 2;
        if (parseInt(this.leaveRecord.days, 0) <= parseInt(this.basicInformation[0].marriage_leave, 0)) {
          this.judge = true;
        }
        break;
      case '产假':
        console.log('产假');
        this.leaveRecord.type = 3;
        if (parseInt(this.leaveRecord.days, 0) <= parseInt(this.basicInformation[0].maternity_leave, 0)) {
          this.judge = true;
          console.log(1234);
          console.log(this.leaveRecord.days);
          console.log(this.basicInformation[0].maternity_leave);
        }
        break;
      case '年假':
        console.log('年假');
        this.leaveRecord.type = 4;
        if (parseInt(this.leaveRecord.days, 0) <= parseInt(this.basicInformation[0].annual_leave, 0)) {
          this.judge = true;
          console.log(1234);
          console.log(this.leaveRecord.days);
          console.log(this.basicInformation[0].annual_leave);
        }
        break;
      default:
        break;
    }
  }
  delete() {
    // noinspection TsLint
    const index = this.approved_LeaveRecords.indexOf(this.selectedLeaveRecord);
    this.logicService.deleteLeaveRecords(this.approved_LeaveRecords[index].id).then(() => {
      this.approved_LeaveRecords = this.approved_LeaveRecords.filter((nmnm, i) => i !== index);
    });
    // 通过outRecords[this.outRecords.indexOf(this.selectedOutRecord)]删除数据库数据
    // console.log(index);
    // console.log(this.outRecords);
    this.leaveRecord = null;
    this.displayDialog = false;
  }
  cloneOutRecord(c: LeaveRecord): LeaveRecord {
    const leaveRecord = {};
    for (const prop in c) {
      if (c.hasOwnProperty(prop)) {
        leaveRecord[prop] = c[prop];
      }
    }
    return leaveRecord;
  }
  constructor( private activateRoute: ActivatedRoute, private logicService: ServiceLogicService) { }

  ngOnInit() {
    this.activateRoute.queryParams.subscribe(params => {
      this.data = params;
    });
    this.logicService.getLeaveRecords(1, this.data.admin_id).then(res => {this.approved_LeaveRecords = res;
      console.log(res);
      for (let i = 0; i < this.approved_LeaveRecords.length; i++) {
        switch (this.approved_LeaveRecords[i].type) {
          case 0:
            this.approved_LeaveRecords[i].type_name = '事假';
            break;
          case 1:
            this.approved_LeaveRecords[i].type_name = '病假';
            break;
          case 2:
            this.approved_LeaveRecords[i].type_name = '婚假';
            break;
          case 3:
            this.approved_LeaveRecords[i].type_name = '产假';
            break;
          case 4:
            this.approved_LeaveRecords[i].type_name = '年假';
            break;
          default:
            break;
        }
      }
      console.log(this.approved_LeaveRecords); });
    this.logicService.getLeaveRecords(5, this.data.admin_id).then(res => {this.rejectAndPass_LeaveRecords = res;
      console.log(this.rejectAndPass_LeaveRecords);
      for (let j = 0; j < this.rejectAndPass_LeaveRecords.length; j++) {
        switch (this.rejectAndPass_LeaveRecords[j].type) {
          case 0:
            this.rejectAndPass_LeaveRecords[j].type_name = '事假';
            break;
          case 1:
            this.rejectAndPass_LeaveRecords[j].type_name = '病假';
            break;
          case 2:
            this.rejectAndPass_LeaveRecords[j].type_name = '婚假';
            break;
          case 3:
            this.rejectAndPass_LeaveRecords[j].type_name = '产假';
            break;
          case 4:
            this.rejectAndPass_LeaveRecords[j].type_name = '年假';
            break;
          default:
            break;
        }
      }
      console.log(this.rejectAndPass_LeaveRecords);
      });
    this.cols = [
      {field: 'satrt_time', header: 'satrt_time'},
      {field: 'end_time', header: 'end_time'},
      {field: 'days', header: 'days'},
      {field: 'reason', header: 'reason'},
      {field: 'type_name', header: 'type_name'},
      {field: 'des', header: 'des'},
    ];
    this.logicService.getBasicInformation(this.data.admin_id).then(res => {
      this.basicInformation = res; console.log(this.basicInformation);  console.log(12344); } );
  }

}
